package com.learn.maven.service;

import com.learn.maven.bean.User;
import com.learn.maven.bean.UserSearchParam;
import com.learn.maven.search.ESService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author wangxing
 * @version 2020/6/10 14:45 Administrator
 */
@Service
public class OrgUserServiceImpl implements InitializingBean {

    @Autowired
    private static ESService elasticOperationService;

    private String index = "test-api";

    private String type = "user";

    public static void main(String[] args) {
        elasticOperationService = new ESService();
        OrgUserServiceImpl orgUserService = new OrgUserServiceImpl();
        try {
            orgUserService.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        elasticOperationService.createIndexIfNotExist(index, type);
    }


    public void batchAddUser(List<User> users) {
        if (CollectionUtils.isEmpty(users)) {
            return;
        }
        for (User user : users) {
            elasticOperationService.addDocumentToBulkProcessor(index, type, user);
        }
    }

    public void addUser(User user) {
        elasticOperationService.addDocument(index, type, user);
    }

    public void deletedUserById(String id) {
        elasticOperationService.deleteDocumentById(index, type, id);
    }

    public void updateUser(User user) {
        String id = user.getId();
        user.setId(null);
        elasticOperationService.updateDocument(index, type, id, user);
    }

//    public List<User> queryByUserName(String userName) {
//
//        UserSearchParam param = new UserSearchParam();
//        param.setUserName(userName);
//        return elasticOperationService.queryDocumentByParam(index, type, param,User.class);
//    }


//    @Override
//    public List<User> queryByRoleName(Role role) {
//        UserSearchParam param = new UserSearchParam();
//        param.setRoleName(role.getName());
//        return elasticOperationService.queryDocumentByParam(index, type, param,User.class);
//    }

}
